package com.tn.rtdata;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A bean that's used to cache information about a fill so that the fill monitor can switch the controlling process when a failure is
 * detected.
 */
public class FillInfo<K, V> implements Serializable
{
  private List<K> contents;
  private FillIdentity fillIdentity;
  private Collection<FillListener<V>> listeners;

  /**
   * Creates a new <code>FillInfo</code>.
   *
   * @param fillIdentity the fill identity.
   * @param contents the fill contents.
   */
  public FillInfo(FillIdentity fillIdentity, List<K> contents, Collection<FillListener<V>> listeners)
  {
    this.fillIdentity = fillIdentity;
    this.contents = new ArrayList<K>(contents);
    this.listeners = new ArrayList<FillListener<V>>(listeners);
  }

  /**
   * Returns the contents.
   */
  public List<K> getContents()
  {
    return contents;
  }

  /**
   * Returns the fill identity.
   */
  public FillIdentity getFillIdentity()
  {
    return fillIdentity;
  }

  /**
   * Returns the listeners.
   */
  public Collection<FillListener<V>> getListeners()
  {
    return listeners;
  }

  /**
   * Compares this object to another for equality.  For another object to be considered equal to a <code>FillInfo</code> the other object
   * must be a <code>FillInfo</code> and must have been initialized with the same values.
   *
   * @param other the other object.
   *
   * @return <code>true</code> if the objects are considered equal, otherwise <code>false</code>.
   */
  @Override
  public boolean equals(Object other)
  {
    return this == other || other != null && getClass() == other.getClass() &&
      new EqualsBuilder()
        .append(getFillIdentity(), ((FillInfo) other).getFillIdentity())
        .append(getContents(), ((FillInfo) other).getContents())
        .append(getListeners(), ((FillInfo)other).getListeners())
        .isEquals();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode()
  {
    return new HashCodeBuilder()
      .append(getFillIdentity())
      .append(getContents())
      .append(getListeners())
      .toHashCode();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString()
  {
    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
      .append("fillIdentity", getFillIdentity())
      .append("contents", getContents())
      .append("listeners", getListeners())
      .toString();
  }
}
